package cn.pn.project.mapper.comment;

import cn.pn.project.pojo.comment.Like;
import org.apache.ibatis.annotations.*;

@Mapper
public interface LikeMapper {
    /**
     * 根据主评论id查询主评论点赞数（用于某个主评论的点赞数量显示）
     * @param commentId 评论id
     * @return 点赞数量
     * @throws Exception
     */
    @Select("SELECT COUNT(1) FROM `like` WHERE commentId=#{commentId};")
    Integer likeCount(@Param("commentId") Integer commentId) throws Exception;

    /**
     * 根据评论id和用户id查找用户是否给该评论点过赞
     * @param like 点赞
     * @return 点赞数量 0 1
     * @throws Exception
     */
    @Select("SELECT COUNT(1) FROM `like` WHERE commentId=#{like.commentId} AND userId=#{like.userId};")
    Integer isLiked(@Param("like") Like like) throws Exception;

    /**
     * 点赞
     * @param like 点赞对象
     * @return 0 1
     * @throws Exception
     */
    @Insert("INSERT INTO `like` VALUES(NULL,#{like.commentId},#{like.userId});")
    @Options(useGeneratedKeys = true, keyProperty = "commentId", keyColumn = "commentId")
    Integer addLike(@Param("like") Like like)throws Exception;

    /**
     * 取消点赞
     * @param like 点赞对象
     * @return 0 1
     * @throws Exception
     */
    @Delete("DELETE FROM `like` WHERE commentId=#{like.commentId} AND userId=#{like.userId}")
    Integer delLike(@Param("like") Like like) throws Exception;

}
